/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.services;

import java.awt.AlphaComposite;
import java.awt.TexturePaint;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;

import org.apache.log4j.Logger;

import who.tmap.util.MiscHelper;

public class TextureService {
	
	public static final int CD_ALPHA_RULE =AlphaComposite.SRC_ATOP;
	public static final float CD_ALPHA_COMPOSITE =0.4f;
	
    private static Logger logger = Logger.getLogger(TextureService.class); 	
	
	// this count does not include the first pattern "none" which is null
	private static final int NB_PATTERNS =5;
	private static final String ICON_PATH ="patterns/pattern_icon";
	private static final String TEXTURE_PATH ="patterns/pattern_texture";
	
	private static final String JPG_EXT =".jpg";
	private static final String GIF_EXT =".gif";
	
	// used for display in combo box
	private static ImageIcon[] iconFillPatterns;
	private static String[] patternNames;	
	private static Integer[] comboBoxIntArray;
	
	// used for texture in shapes
	private static BufferedImage[] imgFillPatterns;
	private static TexturePaint[] texturePaint;
	
	private static TextureService instance =null;
	
	private TextureService() {
		loadPatterns();
	}
	
	public static synchronized TextureService getInstance() {
		if ( instance == null ) {
			instance =new TextureService();
		}
		
		return instance;
	}
	
	public int getNbTextures() {
		return NB_PATTERNS + 1;
	}
	
	public ImageIcon[] getImageIcons() {
		return iconFillPatterns;
	}
	
	public String[] getPatternNames() {
		return patternNames;
	}
	
	public TexturePaint[] getTexturePaint() {
		return texturePaint;
	}	
	
	public Integer[] getComboBoxIntArray() {
		return comboBoxIntArray;
	}
	
	public int getPatternIdxForPatternName( String patternName ) {
		int idx =0;
		
		for( int ct =0; ct < patternNames.length; ct++ ) {
			if ( patternNames[ ct ].equals( patternName ) ) {
				idx =ct;
				break;
			}
		}
		
		return idx;
	}	
	
	private void loadPatterns() {
		MiscHelper miscUIHelper =new MiscHelper();
		
		iconFillPatterns =new ImageIcon[ NB_PATTERNS + 1 ];
		patternNames =new String[ NB_PATTERNS + 1 ];
		comboBoxIntArray =new Integer[  NB_PATTERNS + 1  ];
		
		imgFillPatterns =new BufferedImage[ NB_PATTERNS + 1 ];
		texturePaint =new TexturePaint[ NB_PATTERNS + 1 ];		
		
		for( int ct =0; ct <= NB_PATTERNS; ct++ ) {
			
			iconFillPatterns[ ct ] =( ct > 0 ? miscUIHelper.getImageIcon( ICON_PATH + ct + GIF_EXT )
								       		 : null );
			
			patternNames[ ct ] =(ct > 0 ? "Pattern " + ct : "None" );
			
			comboBoxIntArray[ ct ] =new Integer( ct );

			try {
				if ( ct > 0 ) {
					imgFillPatterns[ ct ] =miscUIHelper.getJPEGBufferedImage( TEXTURE_PATH + ct + JPG_EXT );
					Rectangle2D rect =new Rectangle2D.Double( 0, 0, imgFillPatterns[ ct ].getWidth(), 
																	imgFillPatterns[ ct ].getHeight() );
				
					texturePaint[ ct ] =new TexturePaint( imgFillPatterns[ ct ], rect );
					
				} else {
					imgFillPatterns[ ct ] =null;
					texturePaint[ ct ] =null;					
				}
				
			} catch (Exception e) {
				logger.error( e );
				imgFillPatterns[ ct ] =null;
				texturePaint[ ct ] =null;
				break;
			}
			
		}
			
	}	
	
}
